package com.edaoe.util;

import com.edaoe.kit.lucene.ArticleResult;
import com.edaoe.kit.lucene.IndexManager;
import com.edaoe.kit.lucene.SearchResult;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.Term;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Dull on 2017/6/17 0017.
 */
public class LuceneUtil {
    private static final String UID = "uid";
    private static final String AID = "aid";
    private static final String TEXT = "text";
    private static final String GLOBAL_UID = "0";

    /**
     * 添加索引
     */
    public static void add(long userId, long articleId, String text) {
        String uid = String.valueOf(userId);
        String aid = String.valueOf(articleId);
        Document doc = new Document();
        doc.add(new StringField(AID, aid, Field.Store.YES));
        doc.add(new TextField(TEXT, text, Field.Store.YES));

        IndexManager.addDocument(uid, doc);
        doc.add(new StringField(UID, uid, Field.Store.YES));
        IndexManager.addDocument(GLOBAL_UID,doc);
    }

    /**
     * 删除用户某篇文章的索引
     *
     * @param userId
     * @param articleId
     */
    public static void delete(long userId,long articleId) {
        String uid=String.valueOf(userId);
        String aid=String.valueOf(articleId);
        Term term = new Term(AID, aid);

        IndexManager.deleteDocuments(uid,term);
        IndexManager.deleteDocuments(GLOBAL_UID,term);
    }

    /**
     * 删除某个用户的全部索引
     *
     * @param userId
     */
    public static void deleteAll(long userId) {
        String uid=String.valueOf(userId);

        IndexManager.deleteAll(uid);

        Term term = new Term(UID, uid);
        IndexManager.deleteDocuments(uid,term);
    }

    /**
     * 更新索引
     *
     * @param userId
     * @param articleId
     * @param text
     */
    public static void update(long userId,long articleId, String text) {
        String uid=String.valueOf(userId);
        String aid=String.valueOf(articleId);

        Term term = new Term(AID, aid);
        Document doc = new Document();
        doc.add(new StringField(AID, aid, Field.Store.YES));
        doc.add(new TextField(TEXT, text, Field.Store.YES));
        IndexManager.updateDocument(uid,term,doc);

        doc.add(new StringField(UID, uid, Field.Store.YES));
        IndexManager.updateDocument(GLOBAL_UID,term,doc);
    }

    /**
     * 用户搜索
     */
    public static ArticleResult search(long userId, String keywords, int pageNumber, int pageSize){
        String uid = String.valueOf(userId);
        SearchResult searchResult=IndexManager.search(uid,keywords,pageNumber,pageSize);

        List<Long> articleIds=new ArrayList<>();
        List<Document> documents=searchResult.getDocuments();
        for (Document document : documents) {
            articleIds.add(Long.parseLong(document.get(AID)));
        }

        ArticleResult result=new ArticleResult();
        result.setCount(searchResult.getCount());
        result.setArticleIds(articleIds);
        return result;
    }

    /**
     * 全局搜索
     */
    public static ArticleResult globeSearch(String keywords, int pageNumber, int pageSize){
        SearchResult searchResult=IndexManager.search(GLOBAL_UID,keywords,pageNumber,pageSize);

        List<Long> articleIds=new ArrayList<>();
        List<Document> documents=searchResult.getDocuments();
        for (Document document : documents) {
            articleIds.add(Long.parseLong(document.get(AID)));
        }

        ArticleResult result=new ArticleResult();
        result.setCount(searchResult.getCount());
        result.setArticleIds(articleIds);
        return result;
    }

}
